package com.leinao.resourcesInfo.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.google.common.collect.Lists;
import com.leinao.resourcesInfo.dao.UserJobBandwidthMapper;
import com.leinao.resourcesInfo.model.PaiUserResourceInfo;
import com.leinao.resourcesInfo.model.UserJobBandwidth;
import com.leinao.resourcesInfo.service.UserJobBandWidthService;

/**
*
*
* created by wangshoufa
* 2018年9月6日 下午3:31:55
*
*/

@Service
public class UserJobBandWidthServiceImpl implements UserJobBandWidthService {
	
	private static final Logger logger = LoggerFactory.getLogger(UserJobBandWidthServiceImpl.class);
	
	@Autowired
	private UserJobBandwidthMapper userJobBandwidthMapper;
	
	@Transactional
	@Override
	public List<UserJobBandwidth> insertList(List<UserJobBandwidth> list) {
		if (logger.isInfoEnabled()) {
			logger.info(" insert lists : " + (list == null ? "null" : list.size()));
		}
		List<UserJobBandwidth> resultList = Lists.newArrayList();

		if (CollectionUtils.isEmpty(list)) {
			return new ArrayList<UserJobBandwidth>();
		}
		
		long currentTimeMillis = System.currentTimeMillis();
		
		try {
			for (UserJobBandwidth info : list) {
				info.setCreateTime(currentTimeMillis);
				info.setUpdateTime(currentTimeMillis);
				Long id = userJobBandwidthMapper.insertSelective(info);
				if(id > 0) {
					resultList.add(info);
				}
			}
			
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		
		if (logger.isInfoEnabled()) {
			logger.info(" insert lists  success : " + (resultList == null ? "null" : resultList.size()));
		}
		
		return resultList;
	}
	
	/**
	 * 根据用户ID、JobName、TaskRoleName、TaskIndex来更新带宽
	 * 
	 */
	@Transactional
	@Override
	public List<UserJobBandwidth> updateList(List<UserJobBandwidth> list) {
		if (logger.isInfoEnabled()) {
			logger.info(" update lists : " + (list == null ? "null" : list.size()));
		}
		List<UserJobBandwidth> resultList = Lists.newArrayList();

		if (CollectionUtils.isEmpty(list)) {
			return new ArrayList<UserJobBandwidth>();
		}
		
		long currentTimeMillis = System.currentTimeMillis();
		
		try {
			for (UserJobBandwidth info : list) {
				info.setUpdateTime(currentTimeMillis);
				int id = userJobBandwidthMapper.updateByUserAndJob(info);
				if(id > 0) {
					resultList.add(info);
				}
			}
			
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		
		if (logger.isInfoEnabled()) {
			logger.info(" update lists  success : " + (resultList == null ? "null" : resultList.size()));
		}
		
		return resultList;
	}

	@Override
	public List<Map<String, BigDecimal>> selectUserBandWidth() {
		List<Map<String, BigDecimal>> list = Lists.newArrayList();
		try {
			list = userJobBandwidthMapper.selectUserBandWidth();
			logger.info("list size: "+list.size());
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return list;
	}

	@Override
	public List<UserJobBandwidth> queryAll() {
		List<UserJobBandwidth> list = Lists.newArrayList();
		try {
			list = userJobBandwidthMapper.queryAll();
			logger.info("list size: "+list.size());
		} catch (Exception e) {
			logger.error(e.getMessage(),e );
		}
		return list;
	}
	
	@Transactional
	@Override
	public int deleteRedundant() {
		int result = 0;
		try {
			result = userJobBandwidthMapper.deleteRedundant();
		} catch (Exception e) {
			logger.error(e.getMessage(),e );
		}
		return result;
	}

}
